import { getOpenId , getUserInfoOpenid , getUserInfoUserid ,getRecoveryMember, createUser, getDoctorInfo,getUserInfoKneeInfo} from "./services/_utils/api"
import {GETSTORAGE, SETSTORAGE} from "./services/storage/storage"
var util = require('./utils/watch.js')
// app.js
App({
  onLaunch(e) {
    wx.login({
      success: res => {
        if (res.errMsg == 'login:ok') {
          const code = res.code
          console.log(code,'code');
          getOpenId({
            code: code
          }).then(res => {
            console.log(res);
            SETSTORAGE('openid', res.data.openid)
            SETSTORAGE('unionid', res.data.unionid)
            const openid = res.data.openid
            const unionid = res.data.unionid
            let obj = {
              kneeMiniprogramOpenId: res.data.openid
            }
            getUserInfoKneeInfo(obj).then(res => {
              console.log(res,'膝盖小程序用户信息');
              if(res.data.code == 500 || res.data.data == null){
                createUser({
                  "serviceOfficialAccountOpenid":"",
                  "miniprogramOpenid":"",
                  "unionid":unionid,
                  "kneeMiniprogramOpenid":openid
              }).then(res => {
                console.log(res,"创建用户");
                SETSTORAGE('userId',res.data.data.userId)
                if(res.data.data.userId){
                  getUserInfoKneeInfo(obj).then(res => {
                    SETSTORAGE('kneeCurrentAgreementVersion',res.data.data.kneeCurrentAgreementVersion)
                    if(res.data.data.kneeAgreementstatus == 0){
                      wx.navigateTo({
                        url: '/pages/agreement/agreement',
                      })
                      return
                    }
                  })
                }
              })
              }else{
                console.log(res,'老用户');
                SETSTORAGE('userId',res.data.data.userId)
                if(res.data.data.kneeAgreementstatus == 0 || res.data.data.kneeCurrentAgreementVersion != res.data.data.kneeSignedAgreementVersion){
                  SETSTORAGE('kneeCurrentAgreementVersion',res.data.data.kneeCurrentAgreementVersion)
                  wx.navigateTo({
                    url: '/pages/agreement/agreement',
                  })
                  return
                }
                if(res.data.data.recoveryMemberInfoId==null){
                  wx.navigateTo({
                    url: '/pages/caseInfo/caseInfo',
                  })
                  return
                }else{
                  SETSTORAGE('recoveryMemberInfoId',res.data.data.recoveryMemberInfoId)
                  const num = res.data.data.medicalRecord.length
                  SETSTORAGE('medicalRecordId',res.data.data.medicalRecord[num-1].medicalRecordId)
                }
               
                getDoctorInfo({recoveryMemberInfoId:res.data.data.recoveryMemberInfoId,userId:res.data.data.userId}).then(res => {
                  if(res.data.code == 50000 || res.data.data == null){
                    wx.navigateTo({
                      url: '/pages/bindDoctor/bindDoctor',
                    })
                    return
                  }
                  wx.switchTab({
                    url: '/pages/recovery/recovery',
                  })
                })
              }
            })
          })
        }
      }
    })
  },
  globalData: {
    userInfo: null,
    devices:[],//搜到的设备
    device1:'',//设备1四元素
    device2:'',//设备2四元素
    degree:'',//度数
    connects:[],//已连接的蓝牙
    maxDegree:100,//最大度数
    minDegree:40,//最小度数
    deviceId1:'',
    deviceId2:'',
    BLEFlag:false,
    searchFlag:false,//搜寻设备中
  },
  util:util
})
